﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Documents;

namespace Model.Events
{
    public class LogEntry
    {
        public Event _Event;

        public LogEntry(Event e)
        {
            _Event = e;
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="TxtBlk"></param>
        /// <returns>A List of Hyperlinks where the first is the Event Type and the second is the Keyword Link</returns>
        public List<Hyperlink> LogFormat(TextBlock TxtBlk)
        {
            String KeyWord = _Event.KeyWord;
            String EventDescription = _Event.EventDescription;
            List<Hyperlink> HyperList = new List<Hyperlink>();
            
            String _Type = _Event.EventType.ToString();
            _Type = "[" + _Type.Replace('_', ' ') + "]";
            Hyperlink LinkEventType = new Hyperlink(new Run(_Type));
            HyperList.Add(LinkEventType);
            TxtBlk.Inlines.Add(LinkEventType);

            TxtBlk.Inlines.Add(new Run(" " + _Event.Date + " > "));

            Hyperlink LinkKeyWord = new Hyperlink(new Run(KeyWord));
            HyperList.Add(LinkKeyWord);

            int Index;
            try
            {
                Index = EventDescription.IndexOf(KeyWord);
            }
            catch (ArgumentNullException e)
            {
                //KeyWord was null
                Console.WriteLine("Exception caught: " + e);
                Index = -1;
            }
            if (Index != -1)
            {
                /*Console.WriteLine("Index = " + Index);
                Console.WriteLine(EventDescription.Substring(0, Index));
                Console.WriteLine(EventDescription.Substring(Index, KeyWord.Length));
                Console.WriteLine(EventDescription.Substring(Index + KeyWord.Length, EventDescription.Length - (Index + KeyWord.Length)));//*/

                TxtBlk.Inlines.Add(new Run(EventDescription.Substring(0, Index)));
                TxtBlk.Inlines.Add(LinkKeyWord);
                TxtBlk.Inlines.Add(new Run(EventDescription.Substring(Index + KeyWord.Length, EventDescription.Length - (Index + KeyWord.Length))));
            }
            else
                TxtBlk.Inlines.Add(new Run(EventDescription));

            TxtBlk.Inlines.Add(new Run("\n"));
            return HyperList;
        }
    }
}
